package com.ibo.htlivecamera.launcher;

import android.app.WallpaperManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;

import com.ibo.htlivecamera.R;
import com.ibo.htlivecamera.base.BaseActivity;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

public class AppsActivity extends BaseActivity {

    @ViewInject(R.id.desktop_linear)
    private LinearLayout linearLayout;

    @ViewInject(R.id.mgv)
    private GridView mGridView;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app);

        //设置背景
        WallpaperManager manager = WallpaperManager.getInstance(this);
        Drawable drawable = manager.getDrawable();
        linearLayout.setBackground(drawable);

        initAppList();

        Intent intent = getIntent();
        if (intent != null) {
            String from = intent.getStringExtra("from");
            if (from != null && from.equals("receiver")) {
                handler.postDelayed(finishRunnuble, 1000 * 15);
            }
        }
    }

    Handler handler = new Handler();
    Runnable finishRunnuble = this::finish;

    private void initAppList() {
        List<AppInfo> appInfos = GetAppList(this);
        DeskTopGridViewBaseAdapter deskTopGridViewBaseAdapter = new DeskTopGridViewBaseAdapter(appInfos, this);
        mGridView.setAdapter(deskTopGridViewBaseAdapter);

        mGridView.setOnItemClickListener((parent, view, position, id) -> {
            Intent intent = getPackageManager().getLaunchIntentForPackage(appInfos.get(position).getPackageName());
            if (intent != null) {
                intent.putExtra("type", "110");
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }

        });
    }

    private List<AppInfo> GetAppList(Context context) {
        List<AppInfo> list = new ArrayList<>();
        PackageManager pm = context.getPackageManager();
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        List<ResolveInfo> activities = pm.queryIntentActivities(mainIntent, 0);
        for (ResolveInfo info : activities) {
            String packName = info.activityInfo.packageName;
            if (packName.equals(context.getPackageName())) {
                continue;
            }
            AppInfo mInfo = new AppInfo();
            mInfo.setIco(info.activityInfo.applicationInfo.loadIcon(pm));
            mInfo.setName(info.activityInfo.applicationInfo.loadLabel(pm).toString());
            mInfo.setPackageName(packName);
            // 为应用程序的启动Activity 准备Intent
            Intent launchIntent = new Intent();
            launchIntent.setComponent(new ComponentName(packName, info.activityInfo.name));
            mInfo.setIntent(launchIntent);
            list.add(mInfo);
        }
        return list;
    }

}
